﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace PrototypeUserControl
{
    public partial class ProductList1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.LoadProducts();
            }
        }

        public void LoadProducts()
        {
            //Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbTemp.mdf;Integrated Security=True;User Instance=True"
            string conStr = "Data Source=72.18.135.49,1533;Initial Catalog=DBGio;Persist Security Info=True;User ID=gio;Password=admin123";

            System.Data.SqlClient.SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection(conStr);

            try
            {
                sqlcon.Open();

                string strSQL = " select * from Products ";

                System.Data.SqlClient.SqlCommand dbcomm = new System.Data.SqlClient.SqlCommand(strSQL, sqlcon);

                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
                da.SelectCommand = dbcomm;

                DataTable dt = new DataTable();
                da.Fill(dt);

                this.dtlist.DataSource = dt;
                this.dtlist.DataBind();

                da.Dispose();
                sqlcon.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                sqlcon.Close();
            }

        }

        private void RemoveRecord(string idProduct)
        {
            //Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbTemp.mdf;Integrated Security=True;User Instance=True"
            string conStr = "Data Source=72.18.135.49,1533;Initial Catalog=DBGio;Persist Security Info=True;User ID=gio;Password=admin123";

            System.Data.SqlClient.SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection(conStr);
            try
            {
                sqlcon.Open();

                string strSQL = "Delete from Products where idProducts=" + idProduct;
                System.Data.SqlClient.SqlCommand dbcomm = new System.Data.SqlClient.SqlCommand(strSQL, sqlcon);

                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
                da.SelectCommand = dbcomm;

                dbcomm.ExecuteNonQuery();
                da.Dispose();
                sqlcon.Close();
                Response.Redirect(this.Request.Path, false);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                sqlcon.Close();
            }
        }

        public void dtlist_ItemCommand(object source, DataListCommandEventArgs e)
        {
            this.HideOpenedControls();
            wucProduct wucProduct1 = (wucProduct)e.Item.FindControl("wucProduct1");
            if (e.Item.ItemType == ListItemType.Footer)
            {
                Button btnNew = (Button)e.Item.FindControl("btnNew");

                btnNew.Visible = !e.CommandName.Equals("NewRegister");
                wucProduct1.HeaterText = "New Register";
                wucProduct1.Visible = e.CommandName.Equals("NewRegister");
            }
            else
            {
                Label lblID = (Label)e.Item.FindControl("lblID");
                if (e.CommandName == "Edit")
                {
                    wucProduct1.IdProduct = lblID.Text;
                    wucProduct1.HeaterText = "Edit";
                    wucProduct1.Visible = true;
                }
                else if (e.CommandName == "Remove")
                {
                    this.RemoveRecord(lblID.Text);
                }
                else
                {
                    wucProduct1.Visible = false;
                }
            }
        }

  

        private void HideOpenedControls()
        {
            this.DataList1.ID  = "oo";
            foreach (DataListItem ditem in this.dtlist.Items)
            {
                wucProduct wucProduct1 = (wucProduct)ditem.FindControl("wucProduct1");
                wucProduct1.Visible = false;
            }
        }
    }
}